package com.huya.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huya.common.context.BaseContext;
import com.huya.common.result.PageResult;
import com.huya.mapper.ReportAttachmentMapper;
import com.huya.mapper.ReportMapper;
import com.huya.pojo.dto.ReportDTO;
import com.huya.pojo.dto.ReportPageQueryDTO;
import com.huya.pojo.dto.ReportProcessingDTO;
import com.huya.pojo.entity.Report;
import com.huya.pojo.entity.ReportAttachment;
import com.huya.pojo.vo.ReportVO;
import com.huya.service.ReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private ReportAttachmentMapper reportAttachmentMapper;

    /**
     * 分页查询举报信息
     * @param reportPageQueryDTO
     * @return
     */
    public PageResult pageQuery(ReportPageQueryDTO reportPageQueryDTO) {
        PageHelper.startPage(reportPageQueryDTO.getPage(), reportPageQueryDTO.getPageSize());
        Page<Report> page = reportMapper.pageQuery(reportPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 添加举报信
     * @param reportDTO
     */
    @Transactional
    public void addReport(ReportDTO reportDTO) {
        Report report = new Report();
        BeanUtils.copyProperties(reportDTO, report);
        Long uid = BaseContext.getCurrentId();
        report.setUid(uid);
        report.setStatus(Report.PENDING);
        report.setCreatedTime(LocalDateTime.now());
        reportMapper.insert(report);

        Long reportId = report.getId();
        List<ReportAttachment> attachments = reportDTO.getAttachments();
        if (attachments != null && attachments.size() > 0) {
            attachments.forEach(attachment -> {
                attachment.setRid(reportId);
            });
            reportAttachmentMapper.insert(attachments);
        }
    }

    /**
     * 根据举报信id查询举报信详情
     * @param id
     * @return
     */
    public ReportVO getById(Long id) {
        Report report = reportMapper.getById(id);
        // 查询举报信的附件信息
        List<ReportAttachment> attachments = reportAttachmentMapper.getByRid(id);
        ReportVO reportVO = new ReportVO();
        BeanUtils.copyProperties(report, reportVO);
        reportVO.setAttachments(attachments);
        return reportVO;
    }

    /**
     * 处理举报信
     * @param reportProcessingDTO
     */
    public void processing(ReportProcessingDTO reportProcessingDTO) {
        Report report =Report.builder()
                .id(reportProcessingDTO.getId())
                .status(Report.PROCESSING)
                .build();
        reportMapper.update(report);
    }


    /**
     * 完成举报信
     * @param id
     */
    public void finish(Long id) {
        //根据id查询举报信
        Report report = reportMapper.getById(id);
        //校验举报信状态是否为处理中
        if (report == null || report.getStatus() != Report.PROCESSING) {
            throw new IllegalArgumentException("举报信状态不正确");
        }
        //修改举报信状态为已完成
        report.setStatus(Report.FINISHED);
        reportMapper.update(report);
    }

    /**
     * 分页查询用户的举报信
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    public PageResult pageQueryForUser(int pageNum, int pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        ReportPageQueryDTO  reportPageQueryDTO = new ReportPageQueryDTO();
        reportPageQueryDTO.setUid(BaseContext.getCurrentId());
        reportPageQueryDTO.setStatus(status);
        Page<Report> page = reportMapper.pageQuery(reportPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

}
